package studentinformationmanagementsystem.service.impl;

import studentinformationmanagementsystem.dao.ClassandgradeDao;
import studentinformationmanagementsystem.dao.impl.ClassandgradDaolmpl;
import studentinformationmanagementsystem.entity.Classandgrade;
import studentinformationmanagementsystem.entity.Page;
import studentinformationmanagementsystem.entity.Student;
import studentinformationmanagementsystem.entity.Teacher;
import studentinformationmanagementsystem.service.ClassandgradeService;
import studentinformationmanagementsystem.service.TeacherService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 苗庆
 * @version 1.0
 */
public class ClassandgradeServicelmpl implements ClassandgradeService {

    ClassandgradeDao classandgradeDao=new ClassandgradDaolmpl();

    /**
     * 添加班级信息
     *
     * @param classandgrade
     */
    @Override
    public int register(Classandgrade classandgrade) {
        return classandgradeDao.insert(classandgrade);
    }



    /**
     *  根据ID更新班级信息
     * @param classandgrade  班级对象
     * @return 受影响的记录行数
     */
    @Override
    public int update(Classandgrade classandgrade) {

        return classandgradeDao.update(classandgrade);
    }


    /**
     *  根据ID删除班级
     * @param classandgrade 班级ID
     * @return 受影响的记录行数
     */
    @Override
    public int delete(Classandgrade classandgrade) {
        return classandgradeDao.delete(classandgrade);
    }


    /**
     * 查询所有班级信息
     * @return 所有班级信息
     */
    @Override
    public List<Classandgrade> queryAll() {
        return classandgradeDao.queryAll();
    }

    /**
     * 根据班级ID查询班级信息
     * @param classandgrade 班级ID
     * @return 班级信息
     */
    @Override
    public Classandgrade queryById(Classandgrade classandgrade) {
        return classandgradeDao.queryById(classandgrade);
    }


    /**
     * 分页查询
     * @param pageNo  查询页码
     * @param pageSize  每页的记录数
     * @return
     */
    @Override
    public Page<Classandgrade> page(int pageNo, int pageSize) {

        /**
         * 当前页码:pageNo
         * 总页码:pageTotal
         * 当前页显示的数量:pageSize
         * 总记录数:pageTotalCount
         * 当前页数据:items
         */

        //访问页面的记录条数的开始行数：当前页码-1*每页展示的记录行数
        int bigin=(pageNo-1)*pageSize;


        //总记录数量
        Integer pageTotalCount=classandgradeDao.queryForPageTotalCount();

        //总页码数量=总记录数/每页记录显示数量，然后判断是否可以被除尽，如果不能除尽，那么总的页码数量就需要+1
        Integer pageTotal=pageTotalCount/pageSize;

        if (pageTotalCount%pageSize!=0){
            pageTotal++;
        }


        //当前页码数据
        List<Classandgrade> items = classandgradeDao.queryForPageItems(bigin, pageSize);
        Page<Classandgrade> objectPage = new Page<Classandgrade>(pageNo,pageTotal,pageSize,pageTotalCount,items);

        return objectPage;
    }



    /**
     * 检查班级名称是否已存在
     * @param classandgrade
     * @return true 班级名称已存在
     */
    @Override
    public boolean isExistsClassandgradeNname(Classandgrade classandgrade) {

        boolean falg=false;

        Classandgrade classandgrade1 = classandgradeDao.queryByClassandgradeNname(classandgrade);

        if (classandgrade1!=null){
            falg=true;
        }

        return falg;

    }


    /**
     * 根据班级名称查询班级信息
     * @param classandgrade
     * @return
     */
    @Override
    public Classandgrade queryName(Classandgrade classandgrade) {
        return classandgradeDao.queryByClassandgradeNname(classandgrade);
    }


    @Override
    public List<Object> getClass(Student student) {
        //用来装班级对象已经查询到的教师对象信息
        List<Object> list=new ArrayList<>();

        //1.根据学生的班级名称查询班级信息
        Classandgrade classandgrade = new Classandgrade();
        classandgrade.setClassandgrade(student.getClassandgrade());

        Classandgrade classandgrade1 = queryName(classandgrade);

        //2.根据班级查询对应的教师信息
        Teacher teacher = new Teacher();
        teacher.setTeacherid(classandgrade1.getTeacherid());
        TeacherService teacherService = new TeacherServiceImpl();

        Teacher teacher1 = teacherService.queryById(teacher);
        //3.将班级信息和教师信息装入对list
        list.add(classandgrade1);
        list.add(teacher1);
        return list;
    }


}
